var app = getApp();
var api = require("../config/api.js")

/*
*上传图片并获取图片地址
*/
function getUploadImg() {
  return new Promise(function (resolve, reject) {
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: api.GoodsImagUpload,
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data",
            'ACCESS-TOKEN': wx.getStorageSync('token')
          },
          formData: {
            'user': 'test'
          },
          success: function (res) {
            var data = JSON.parse(res.data)
            console.log("data=" + data)
            if (data.success) {
              resolve(data.obj.url); 
            } else {
              wx.showToast({
                title: '图片上传失败，请检查您的网络',
                icon: 'none'
              })
            }
          }, fail: function () {
            wx.showToast({
              title: '失败，请检查您的网络',
              icon: 'none'
            })
          }
        })
      }
    })
  });
}


/**
 * wx.request的异步封装
 */
function request(url, data = {}, method = "") {
  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        resolve(res.data);
      },
      fail: function (res) {
        reject(res)
      }
    })
  });
}

// comm.request(api.GetGoodInfomanage, {}, "").then(function (res) {
//   wx.showLoading({
//     title: '加载中',
//   })
//   if (res.success) {

//   }
// }).catch(function (res) {
//   console.log("获取数据失败！")
// })

/*
 * 登录用户功能使用的权限
 */
function authjust(con) {
  var admin = wx.getStorageSync("usertype");
  if (admin == 4) {
    var poewr = wx.getStorageSync("poewr");
    if (poewr) {
      return true;
    } else {
      wx.showModal({
        title: '提示',
        content: '抱歉！您暂时无法使用该功能，请先前往商家中心申请设备！',
        confirmColor: "#43CBC9",
        success: (res) => {
          wx.showLoading({
            title: '加载中',
          })
          if (res.confirm) {
            wx.hideLoading();
            wx.navigateTo({
              url: '/pages/devapply/devapply',
            })
          } else if (res.cancel) {
            wx.hideLoading();
            return false;
          }
        }
      })
      return false;
    }
  } else if (admin == 5) {
    if (con == 1) {
      wx.showToast({
        title: "抱歉！您的权限无法使用该功能！",
        icon: 'none',
        duration: 2000
      });
      return false;
    } else if (con == 2) {
      return true;
    }

  }
}

/*
 *商家激活使用功能的权限
 */
function getpower(e) {
  var that = this
  var admin = wx.getStorageSync("usertype");
  if (admin == 4) {
    wx.setStorageSync("adminer", '商家')
    var status = wx.getStorageSync('status')
    if (status == 1) {
      wx.setStorageSync("poewr", true)
    } else {
      wx.setStorageSync("poewr", false)
    }
  } else if (admin == 5) {
    wx.setStorageSync("adminer", '技师')
  }
}

function transDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1

  var day = date.getDate();
  return [year, month, day].map(formatNumber).join('-')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}


module.exports = {
  request,
  authjust,
  getpower,
  getUploadImg,
  transDate
}